//
//  CYTicketNoticeViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/9.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYTicketNoticeViewController.h"
#import "CYWinningTicketCell.h"
#import "CYSingleTicketViewController.h"
#import "CYTotalLotteryNoticeModel.h"
@interface CYTicketNoticeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) CYTotalLotteryNoticeModel *model;
@end

@implementation CYTicketNoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
}

- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] init];
        _tableview.showsVerticalScrollIndicator = NO;
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.mj_header = [CYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshDown)];//下拉刷新
        [self.view addSubview:_tableview];
        
        [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(NAVIBAR_HEIGHT + STATUS_HEIGHT);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - 37.5));
            make.centerX.equalTo(self.view);
        }];
    }
    return _tableview;
}

- (void)setModel:(CYTotalLotteryNoticeModel *)model{
    if (_model != model) {
        _model = model;
        if (!_tableview)
            self.tableview.hidden = NO;
        else
            [self.tableview reloadData];
    }
}
//下拉刷新
- (void)refreshDown{
    [self https];
}

#pragma - mark - UITableViewDataSource method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _model.notices.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = @"CYWinningTicketCell";
    CYWinningTicketCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
        cell = [[CYWinningTicketCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    CYLotteryNoticeModel *subModel = _model.notices[indexPath.row];
    cell.name = subModel.name;
    cell.action_no = subModel.action_no;
    cell.action_data_time = subModel.action_data_time;
    cell.data = subModel.action_data;
    return cell;
    
}
#pragma - mark - UITableViewDelegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CYSingleTicketViewController *ticketVC = [[CYSingleTicketViewController alloc] init];
    ticketVC.lotteryNoticeModel = _model.notices[indexPath.row];//彩种ID
    [self.navigationController pushViewController:ticketVC animated:YES];
}

- (void)https{
    self.task = [CYHttps POST:API_result parameters:nil showHUD:YES progress:nil success:^(id data) {
        if (_tableview)
            [_tableview.mj_header endRefreshing];
        self.model = [CYTotalLotteryNoticeModel mj_objectWithKeyValues:@{@"notices":data}];
    } failure:nil];
    
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self https];
}
- (void)setupNavigationBarTintColor:(UIColor *)color{
    [super setupNavigationBarTintColor:color];
    self.navigationItem.title = @"开奖公告";
    self.navigationItem.leftBarButtonItem = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
